package com.houdoudou.common;

import java.sql.Connection;
import java.util.List;

import com.houdoudou.bean.Order;
import com.houdoudou.bean.OrderProduct;
import com.houdoudou.bean.Refund;
import com.houdoudou.dao.OrderDao;
import com.houdoudou.dao.OrderProductDao;
import com.houdoudou.dao.RefundDao;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class CommonSql {

 public static	JSONObject selectOrderRelated(Connection conn , long id){
		JSONObject result=null;
		
		 OrderDao orderDao= new OrderDao(conn);
         OrderProductDao orderProductDao= new OrderProductDao(conn);
         Order order;
		try {
			order = orderDao.selectById(id);
			if(order==null){ return null;}
            result= JSONObject.fromObject(order);
         	List<OrderProduct>  orderProducts=  orderProductDao.selectById(order.getId()); 
   	       result.put("products", JSONArray.fromObject(orderProducts));
   	       
   	       if(order.getIsRefund()==2){
   	    	  Refund refund=new RefundDao(conn).selectById(id);  
			  result.put("refund", JSONObject.fromObject(refund));
   	       }
		} catch (Exception e) {
	         
			e.printStackTrace(); return null;
		}
      
		return result;
	}
}
